package com.openxt.nbzj.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.openxt.nbzj.R;
import com.openxt.nbzj.fragment.OpenxtBaseActivity;
import com.openxt.nbzj.model.ConfigModel;
import com.openxt.nbzj.model.RemotingModel;
import com.openxt.nbzj.remoting.RemotingEx;

import butterknife.BindView;
import butterknife.ButterKnife;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import qs.openxt.libs.progress.Wait;
import qs.openxt.libs.util.StringUtil;
import qs.openxt.libs.util.Utils;

/**
 * Created by 颖杰_2 on 2016-07-30.
 */
public class EnterpriseDetailsActivity extends OpenxtBaseActivity implements RemotingEx.OnActionListener {
    @BindView(R.id.EnterpriseName)
    TextView EnterpriseName;
    @BindView(R.id.OrgCode)
    TextView OrgCode;
    @BindView(R.id.Introduction)
    TextView Introduction;
    @BindView(R.id.Address)
    TextView Address;
    @BindView(R.id.Owner)
    TextView Owner;
    @BindView(R.id.Phone)
    TextView Phone;
    @BindView(R.id.Qualificationlst)
    LinearLayout Qualificationlst;

    Handler mHandler = new Handler();
    Context mContext = null;
    RemotingModel remotingModel = null;
    String ID = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setBaseStyle(R.layout.enterprise_detail_activity, "企业资质详情");
        ButterKnife.bind(this);
        setActionUrl("#/enterprice");
        mContext = this;
        remotingModel = new RemotingModel(mContext);

        Bundle bundle = getIntent().getExtras();
        if(bundle == null)
            return;
        ID = bundle.getString("ID");

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                initDetail();
            }
        }, 100);
    }

    private void initDetail(){
        String val = Utils.getLocalStorage(mContext, ConfigModel.ENTERPRISE_GETINFO + "$" + ID);
        if(!StringUtil.isNull(val)){
            initDetailView(val);
            remotingModel.doEnterpriseGetinfo(this, false, ID);
        }else{
            Wait.sharedInstance().start(mContext, "正在获取详情..., 请稍候");
            remotingModel.doEnterpriseGetinfo(this, false, ID);
        }
    }

    private void initDetailView(String val){
        if(StringUtil.isNull(val))
            return;
        try{
            JSONObject info = JSON.parseObject(val);
            String $EnterpriseName = info.getString("EnterpriseName");
            if(StringUtil.isNull($EnterpriseName))
                $EnterpriseName = "无";
            EnterpriseName.setText("\u3000\u3000" + $EnterpriseName);

            String $OrgCode = info.getString("OrgCode");
            if(StringUtil.isNull($OrgCode))
                $OrgCode = "无";
            OrgCode.setText($OrgCode);

            String $Introduction = info.getString("Introduction");
            if(StringUtil.isNull($Introduction))
                $Introduction = "无";
            Introduction.setText($Introduction);

            String $Owner = info.getString("Owner");
            if(StringUtil.isNull($Owner))
                $Owner = "无";
            Owner.setText($Owner);

            String $Address = info.getString("Address");
            if(StringUtil.isNull($Address))
                $Address = "无";
            Address.setText($Address);

            String $Phone = info.getString("Phone");
            if(StringUtil.isNull($Phone))
                $Phone = "无";
            Phone.setText($Phone);

            LayoutInflater inflater = LayoutInflater.from(mContext);
            JSONArray $Qualificationlst = info.getJSONArray("Qualificationlst");
            Qualificationlst.removeAllViews();
            for(int i = 0, j = $Qualificationlst.size(); i < j; i++){
                JSONObject item = $Qualificationlst.getJSONObject(i);
                String QType = item.getString("QType");
                RelativeLayout item_01 = (RelativeLayout)inflater.inflate(R.layout.qualification_item_01, null);
                TextView tv01 = (TextView)item_01.findViewById(R.id.type_name);
                tv01.setText(QType);
                Qualificationlst.addView(item_01);

                String QName = item.getString("QName");
                Qualificationlst.addView(getItem(inflater, "资质名称", QName));
                String QLevel = item.getString("QLevel");
                Qualificationlst.addView(getItem(inflater, "资质等级", QLevel));
                String QLicense = item.getString("QLicense");
                Qualificationlst.addView(getItem(inflater, "资质证书编号", QLicense));
                String QIssue = item.getString("QIssue");
                Qualificationlst.addView(getItem(inflater, "发证机关", QIssue));
                String QData = item.getString("QData");
                Qualificationlst.addView(getItem(inflater, "有效日期", QData));
            }

            Qualificationlst.invalidate();
        }catch (Exception e){

        }
    }

    private RelativeLayout getItem(LayoutInflater inflater, String arg0, String arg1){
        RelativeLayout item = (RelativeLayout)inflater.inflate(R.layout.qualification_item_02, null);
        TextView tv01 = (TextView)item.findViewById(R.id.left_name);
        tv01.setText(arg0);
        TextView tv02 = (TextView)item.findViewById(R.id.right_name);
        tv02.setText(arg1);
        return item;
    }

    @Override
    public void onActionCompleted(String reVal, String actionName) {
        Wait.sharedInstance().stop();
        if(actionName.equals(ConfigModel.ENTERPRISE_GETINFO + "$" + ID)){
            Utils.setLocalStorage(mContext, ConfigModel.ENTERPRISE_GETINFO + "$" + ID, reVal);
            initDetailView(reVal);
        }
    }

    @Override
    public void onActionFailed(String errorVal, String actionName) {
        Wait.sharedInstance().stopAll();
        showMsg(errorVal);
    }
}
